import Vue from 'vue'//引入vue
import VueRouter from 'vue-router'//引入vue-router依赖
Vue.use(VueRouter);//把vueRouter依赖注入到vue里面

// import Home from '../views/home.vue'//引入页面
// import study from '../views/study.vue'//引入页面
// import control from '../views/control.vue'//引入页面
import work from '../views/work.vue'//引入页面
const routes = [
    // {
    //   path:'/home',
    //   name:'home',
    //   component:Home,
    // },
    // {
    //   path:'/control',
    //   name:'control',
    //   component:control,
    // },
    // {
    //   path:'/study',
    //   name:'study',
    //   component:study,
    //   children:[//子路由路由懒加载的方式
    //     {
    //       path:'class',
    //       name:'class',
    //       component:() => import('../views/class.vue')
    //     },
    //     {
    //       path:'curriculum',
    //       name:'curriculum',
    //       component:() => import('../views/curriculum.vue')
    //     },
    //     {
    //       path:'task',
    //       name:'task',
    //       component:() => import('../views/task.vue')
    //     },
    //     {
    //       path:'core',
    //       name:'core',
    //       component:() => import('../views/core.vue'),
    //     }
    //   ]
    // },
    {
      path:'/',
      name:'work',
      component:work,
      redirect:{name:'/work/home'},
      children:[//子路由路由懒加载的方式
        {
          path:'class',
          name:'/work/class',
          component:() => import('../work/class.vue')
        }, {
          path:'/home',
          name:'/work/home',
          component:() => import('../work/home.vue')
        },
        {
          path:'curriculum',
          name:'/work/curriculum',
          component:() => import('../work/curriculum.vue')
        },
        {
          path:'task',
          name:'/work/task',
          component:() => import('../work/task.vue')
        },
        {
          path:'core',
          name:'/work/core',
          component:() => import('../work/core.vue'),
        },
        {
          path:'/control',
          name:'/work/control',
          component:() => import('../work/control.vue'),
        },
      ]
    },
    {
      path:'/login',
      name:'login',
      component:()=>import('../work/login.vue'),
      beforeEnter:(to, from, next)=>{//独有的守卫
        console.log('路由独享守卫', to)
        console.log('路由独享守卫', from)
        next()
      }
    }
    
]
const router = new VueRouter({
    mode:'history',
    base:'/',
    routes//声明路由表
  });
  
  router.beforeEach((to, from, next)=>{
    console.log('beforeEach',to)
    console.log('beforeEach',from)
    // next()//拦截 正常怎么跳转就是怎么跳转 不拦截
    // next(false)//拦截 取消掉本次导航回到跳转之前的页面
    // let isLogn = false;
    // let isLogin = localStorage.getItem('isLogin');
    if(to.path.indexOf('/work/task') !=-1){
     next({
       name:'login'
     })
    }else{
      next()
    }
    // if(! isLogin ){//如果不是跳转到login不管那个页面都自动跳转到login
    //   next({
    //     name:'login'
    //   })
    //   }else{
    //     next()
    // }  
  })//全局前置守卫
  
  router.beforeResolve((to, from, next)=>{//导航确定之前
    console.log('beforeResolve',to)
    console.log('beforeResolve',from)
    next();
  })

  router.afterEach((to, from)=>{//导航行为确认之后无法拦截导航只能接受导航的参数
    console.log('afterEach',to)
    console.log('froafterEachm',from)
  })

  export default  router //导出路由对象